<?php

namespace app\services;

use app\models\Group;
use yii;

/**
 * GroupService业务逻辑类
 * @author administrator
 * @since 2017-07-10
 */
class GroupService  
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 判断用户组是否存在
     * @param $groupName
     * @param int $groupId
     * @return bool
     */
    public function checkGroupNameIsExists($groupName, $groupId = 0)
    {
        $groupModel = new Group();
        $param      = [
            'where' => [
                'and',
                'groupName=:groupName'
            ],
            'bind'  => [
                ':groupName' => $groupName
            ]
        ];
        if ($groupId) {
            $param['where'][]     = 'id!=:id';
            $param['bind'][':id'] = $groupId;
        }
        $groupCheck = $groupModel->dbGet('*', '', $param);
        if (!empty($groupCheck)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 删除用户组数据
     * @param $id
     * @return mixed
     */
    public static function deleteGroup($id)
    {
        $groupModel = new Group();
        $param      = [
            'where' => [
                'and',
                'id=:id'
            ],
            'bind'  => [
                ':id' => $id
            ]
        ];
        $check      = $groupModel->dbGet('*', '', $param);
        if (empty($check)) {
            $return['code']    = 1001;
            $return['message'] = '找不到相关数据';
        } else {
            try {
                $result = $groupModel->dbDel('', ['where' => 'id=:id', 'bind' => [':id' => $id]]);
                if ($result) {
                    $return['code']    = 1000;
                    $return['message'] = '删除成功!';
                } else {
                    $return['code']    = 1001;
                    $return['message'] = '删除失败!';
                }
            } catch (Exception $e) {
                $return['code']    = 1001;
                $return['message'] = '数据删除失败';
            }
        }
        return $return;
    }
}
